<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>
          <el-form-item label="活动类型" prop="activityType">
            <el-select v-model="queryParams.activityType" placeholder="请选择活动类型" clearable size="small">
              <el-option v-for="dict in activityTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
          <el-form-item label="活动名称" prop="activityName">
            <el-input v-model="queryParams.activityName" placeholder="请输入活动名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="参与状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择参与状态" clearable size="small">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item style="margin-left: 22px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['activity:join:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['activity:join:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['activity:join:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['activity:join:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<div class="divTable">
    <el-table border v-loading="loading" :data="joinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动类型" align="center" prop="activityType" :formatter="activityTypeFormat"  />
      <el-table-column label="活动名称" align="center" prop="activityName" :show-overflow-tooltip="true"  />
      <el-table-column label="客户头像" align="center" prop="customerUrl">
        <template slot-scope="scope">
          <el-image v-if="scope.row.customerUrl != null && scope.row.customerUrl != '' " slot="reference"
              :src="scope.row.customerUrl" class="image-rows" :preview-src-list="[scope.row.customerUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" width="120" :show-overflow-tooltip="true"  />
      <!-- <el-table-column label="" align="center" prop="proImg" :show-overflow-tooltip="true"  /> -->
      <el-table-column label="商品图片" align="center" prop="proImg" width="110">
        <template slot-scope="scope">
          <el-image v-if="scope.row.proImg != null && scope.row.proImg != '' " slot="reference"
              :src="scope.row.proImg" class="image-rows" style="width: 80px;height: 80px;" :preview-src-list="[scope.row.proImg]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" min-width="150" prop="proName" />
      <el-table-column label="SKU名称" align="center" prop="skuName" :show-overflow-tooltip="true"  />
      <el-table-column label="商品数量" align="center" prop="proNumber" :show-overflow-tooltip="true"  />
      <el-table-column label="活动价格" align="center" prop="activityPriceStr" :show-overflow-tooltip="true"  />
      <el-table-column label="成交价格" align="center" prop="buyPriceStr" :show-overflow-tooltip="true"  />
      <el-table-column label="已砍金额" align="center" prop="bargainAmountStr" :show-overflow-tooltip="true"  />
      <el-table-column label="参与状态" align="center" prop="dataStatusName"   />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单ID" align="center" prop="orderUid" :show-overflow-tooltip="true"  /> -->
      <el-table-column label="过期时间" align="center" prop="expireDate" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['activity:join:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:join:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['activity:join:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
</div>
    <!-- 添加或修改活动参与数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="参与活动父级ID" prop="joinPuid">
          <el-input v-model="form.joinPuid" placeholder="请输入参与活动父级ID" />
        </el-form-item>
        <el-form-item label="活动ID" prop="activityUid">
          <el-input v-model="form.activityUid" placeholder="请输入活动ID" />
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="form.activityType" placeholder="请选择活动类型">
            <el-option v-for="dict in activityTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动商品ID" prop="activityProUid">
          <el-input v-model="form.activityProUid" placeholder="请输入活动商品ID" />
        </el-form-item>
        <el-form-item label="商品ID" prop="proUid">
          <el-input v-model="form.proUid" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="商品名称" prop="proName">
          <el-input v-model="form.proName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品图片" prop="proImg">
          <el-input v-model="form.proImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="sku属性ID" prop="skuUid">
          <el-input v-model="form.skuUid" placeholder="请输入sku属性ID" />
        </el-form-item>
        <el-form-item label="SKU名称" prop="skuName">
          <el-input v-model="form.skuName" placeholder="请输入SKU名称" />
        </el-form-item>
        <el-form-item label="商品数量" prop="proNumber">
          <el-input v-model="form.proNumber" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="客户ID" prop="customerUid">
          <el-input v-model="form.customerUid" placeholder="请输入客户ID" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户头像" prop="customerUrl">
          <el-input v-model="form.customerUrl" placeholder="请输入客户头像" />
        </el-form-item>
        <el-form-item label="活动折扣" prop="activityRate">
          <el-input v-model="form.activityRate" placeholder="请输入活动折扣" />
        </el-form-item>
        <el-form-item label="活动折扣后的价格" prop="activityPrice">
          <el-input v-model="form.activityPrice" placeholder="请输入活动折扣后的价格" />
        </el-form-item>
        <el-form-item label="成交价格" prop="buyPrice">
          <el-input v-model="form.buyPrice" placeholder="请输入成交价格" />
        </el-form-item>
        <el-form-item label="已砍金额" prop="bargainAmount">
          <el-input v-model="form.bargainAmount" placeholder="请输入已砍金额" />
        </el-form-item>
        <el-form-item label="参与状态" prop="dataStatus">
          <el-select v-model="form.dataStatus" placeholder="请选择参与状态">
            <el-option v-for="dict in dataStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单ID" prop="orderUid">
          <el-input v-model="form.orderUid" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="过期时间" prop="expireDate">
          <el-date-picker clearable size="small" v-model="form.expireDate" type="date" value-format="yyyy-MM-dd"
            placeholder="选择过期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看活动参与数据对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listJoin,
    getJoin,
    delJoin,
    addJoin,
    updateJoin
  } from "@/api/activity/join";

  export default {
    name: "Join",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 活动参与数据表格数据
        joinList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        // 活动类型字典
        activityTypeOptions: [],
        // 参与状态字典
        dataStatusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          joinPuid: null,
          activityUid: null,
          activityType: null,
          activityProUid: null,
          proUid: null,
          proName: null,
          proImg: null,
          skuUid: null,
          skuName: null,
          proNumber: null,
          customerUid: null,
          customerName: null,
          customerUrl: null,
          activityRate: null,
          activityPrice: null,
          buyPrice: null,
          bargainAmount: null,
          dataStatus: null,
          updateTime: null,
          createTime: null,
          orderUid: null,
          expireDate: null,
          activityName:null
        },
        columns: [{
            label: '过期时间',
            dataIndex: 'joinUid',
          },
          {
            label: '参与活动父级ID',
            dataIndex: 'joinPuid',
          },
          {
            label: '活动ID',
            dataIndex: 'activityUid',
          },
          {
            label: '活动类型',
            dataIndex: 'activityType',
          },
          {
            label: '活动商品ID',
            dataIndex: 'activityProUid',
          },
          {
            label: '商品ID',
            dataIndex: 'proUid',
          },
          {
            label: '商品名称',
            dataIndex: 'proName',
          },
          {
            label: '商品图片',
            dataIndex: 'proImg',
          },
          {
            label: 'sku属性ID',
            dataIndex: 'skuUid',
          },
          {
            label: 'SKU名称',
            dataIndex: 'skuName',
          },
          {
            label: '商品数量',
            dataIndex: 'proNumber',
          },
          {
            label: '客户ID',
            dataIndex: 'customerUid',
          },
          {
            label: '客户名称',
            dataIndex: 'customerName',
          },
          {
            label: '客户头像',
            dataIndex: 'customerUrl',
          },
          {
            label: '活动折扣',
            dataIndex: 'activityRate',
          },
          {
            label: '活动折扣后的价格',
            dataIndex: 'activityPrice',
          },
          {
            label: '成交价格',
            dataIndex: 'buyPrice',
          },
          {
            label: '已砍金额',
            dataIndex: 'bargainAmount',
          },
          {
            label: '参与状态',
            dataIndex: 'dataStatus',
          },
          {
            label: '更新时间',
            dataIndex: 'updateTime',
          },
          {
            label: '创建时间',
            dataIndex: 'createTime',
          },
          {
            label: '订单ID',
            dataIndex: 'orderUid',
          },
          {
            label: '过期时间',
            dataIndex: 'expireDate',
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          activityUid: [{
            required: true,
            message: "活动ID不能为空",
            trigger: "blur"
          }],
          proUid: [{
            required: true,
            message: "商品ID不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("activity_activity_type").then(response => {
        this.activityTypeOptions = response.data;
      });
      this.getDicts("activity_join_status").then(response => {
        this.dataStatusOptions = response.data;
      });
    },
    methods: {
      /** 查询活动参与数据列表 */
      getList() {
        this.loading = true;
        listJoin(this.queryParams).then(response => {
          this.joinList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 活动类型字典翻译
      activityTypeFormat(row, column) {
        return this.selectDictLabel(this.activityTypeOptions, row.activityType);
      },
      // 参与状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          joinUid: null,
          joinPuid: null,
          activityUid: null,
          activityType: null,
          activityProUid: null,
          proUid: null,
          proName: null,
          proImg: null,
          skuUid: null,
          skuName: null,
          proNumber: null,
          customerUid: null,
          customerName: null,
          customerUrl: null,
          activityRate: null,
          activityPrice: null,
          buyPrice: null,
          bargainAmount: null,
          dataStatus: null,
          updateTime: null,
          createTime: null,
          orderUid: null,
          expireDate: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.joinUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加活动参与数据";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看活动参与数据";
        this.showData = [];
        this.columns.forEach(item => {
          this.showData.push({
            label: item.label,
            value: row[item.dataIndex]
          })
        })
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const joinUid = row.joinUid || this.ids
        getJoin(joinUid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改活动参与数据";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.joinUid != null) {
              updateJoin(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJoin(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const joinUids = row.joinUid || this.ids;
        this.$confirm('是否确认删除活动参与数据编号为"' + joinUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delJoin(joinUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('activity/join/export', {
          ...this.queryParams
        }, `activity_join.xlsx`)
      }
    }
  };
</script>
